﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=LoaiKhuyenMai company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using KM.BUS;
using KM.ENT;
using KM.Models;
using CORE;
using HtmlAgilityPack;

namespace KM.Areas.Admin.Controllers
{
    public class LoaiKhuyenMaiController : Controller
    {
        int pageSize = 20;
        
        public ActionResult dsLoaiKhuyenMai()
        {
            //lay danh sach loai san pham
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            lst = busLKM.getAll(ref page);
            ViewBag.page = page;
            return View(lst);
        }
        
        public ActionResult dsLoaiKhuyenMaiByForeignKey(int Id)
        {
            //lay danh sach loai san pham
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            lst = busLKM.getAllByForeignKey(Id, ref page);
            ViewBag.page = page;
            return View(lst);
        }
        
        public ActionResult edit(int Id = 0)
        {
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            entLKM = busLKM.getInfo(Id);
            return View(entLKM);
        }
        
        public ActionResult delete(int Id)
        {
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            busLKM.delete(Id);
            return RedirectToAction("dsLoaiKhuyenMai", null, new { Id = 1 });
        }
        [HttpPost]
        
        [ValidateInput(false)]
        public ActionResult edit(entLoaiKhuyenMai entLKM)
        {
            if (!ModelState.IsValid)
            {
                return View(entLKM);
            }

            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            busLKM.createEdit(entLKM);
            return RedirectToAction("edit", new { Id = entLKM.LoaiKhuyenMaiId });


        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetProduct()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetProduct(FormCollection collection)
        {
            //http://remthinhphat.com/rem-vai.html?page=1
            int k = 0;
            string url = collection["Url"].ToString();
            string nodeName = "a[@class=\"tensp\"]";
            string nodeImage = "a/img";
            HtmlWeb htmlWeb = new HtmlWeb();
            // Creates an HtmlDocument object from an URL
            HtmlAgilityPack.HtmlDocument document = htmlWeb.Load(url);
            var nodes = from foo in document.DocumentNode.Descendants("div")
                        where foo.Attributes["class"] != null && foo.Attributes["class"].Value.Contains("sp")
                        select foo;
            int i = 2;
            string a = "";
            if (nodes != null)
            {
                foreach (HtmlAgilityPack.HtmlNode item in nodes)
                {
                    HtmlNode img = item.SelectSingleNode(nodeImage);
                    string filename = DateTime.Now.ToString("ddmmyyhhmmss").ToString() + k++.ToString() + ".jpg";
                    if (img != null)
                    {
                        Utils.DownloadImageFromUrl(img.Attributes["src"].Value, filename);

                        HtmlNode Name = item.SelectSingleNode(nodeName);
                        if (Name != null)
                            //a += Name.InnerText;
                            a = string.Format("insert into SanPham(TenSanPham,Image,LoaiSanPhamId) values(N'{0}','{1}',{2})", Name.InnerText, filename, 1);
                    }
                    FileTool.Log(a);


                }

            }
            ViewBag.me = a;
            return View();

        }

        //lay danh sach loai san pham by loai san pham cha id
        public PartialViewResult GetMenuByChaId(int id)
        {
            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            lst = busLKM.getAllByChaId(id);
            return PartialView(lst);
        }
        public PartialViewResult GetMenuCon(int id)
        {

            List<entLoaiKhuyenMai> lst = new List<entLoaiKhuyenMai>();
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            lst = busLKM.getAllByChaId(id);
            return PartialView(lst);
        }
        
      
    }
}
